<%--
  Created by IntelliJ IDEA.
  User: 86182
  Date: 2022/8/30
  Time: 13:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <!-- 引入 layui.css -->
    <link href="//unpkg.com/layui@2.7.6/dist/css/layui.css" rel="stylesheet">
    <!-- 引入 layui.js -->
    <script src="//unpkg.com/layui@2.7.6/dist/layui.js"></script>
<body>
<script type="text/html" id="barDemo1">
<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="update">编辑</a>
<a class="layui-btn layui-btn-xs layui-btn-danger"  lay-event="delete">删除</a>
</script>
<%--条件查询表--%>
<form class="layui-form" id="haha" action="" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-inline">
            <input type="text" name="question" placeholder="请输入问题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-inline">
            <input type="text" name="create_by" placeholder="请输入创建人" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="conditionalQuery"><i class="layui-icon layui-icon-search"
                                                                                  style="font-size: 20px;"></i>查询
            </button>
            <button type="test" class="layui-btn layui-btn-primary" id="reset">重置</button>
        </div>
    </div>
</form>
<script type="text/html" id="barDemo">
    <a class="layui-btn" type="add" lay-event="add"><i class="layui-icon layui-icon-add-circle"
                                                       style="font-size: 20px;"></i>添加</a>
    <a class="layui-btn layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-reduce-circle"
                                                             style="font-size: 20px;"></i>删除</a>
</script>
<table id="demo" lay-filter="test"></table>
<%--添加表格--%>
<form class="layui-form" action="" id="addForm" style="display: none" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">请输入题目</label>
        <div class="layui-input-block" style="width: 350px">
            <input type="text" name="question" id="questionAdd" required  lay-verify="required" placeholder="请输入内容" autocomplete="off" class="layui-input">
            <span style="color: red"></span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-inline" style="width: 350px">
            <textarea name="optionA" required lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
            <label class="layui-form-label">选项B</label>
            <div class="layui-input-inline" style="width: 350px">
                <textarea name="optionB" required lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选项C</label>
                <div class="layui-input-inline" style="width: 350px">
                    <textarea name="optionC" required lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">选项D</label>
                    <div class="layui-input-inline" style="width: 350px">
                        <textarea name="optionD" required lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">选择框</label>
                    <div class="layui-input-block">
                        <select name="typ" lay-verify="required">
                            <option  value="1">前十五题</option>
                            <option value="2">后十五题</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态:</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="status" value="0" lay-skin="switch" lay-text="启用|禁用" checked>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">是否展示</label>
                    <div class="layui-input-block">
                        <input type="radio" name="flag" value="0" title="是" checked>
                        <input type="radio" name="flag" value="1" title="否" >
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="addFilter">立即提交</button>
                        <button type="test" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<%--修改表格--%>
<form class="layui-form" action="" id="updateForm" lay-filter="updateFormFilter" style="display: none" enctype="multipart/form-data">
    <div class="layui-form-item" style="display: none" >
        <label class="layui-form-label">id</label>
        <div class="layui-input-block">
            <input type="text" name="id" required  lay-verify="required" placeholder="请输入内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">请输入题目</label>
        <div class="layui-input-block" style="width: 350px">
            <textarea name="question" required lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-inline" style="width: 350px">
            <textarea name="optionA" required lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选项B</label>
            <div class="layui-input-inline" style="width: 350px">
                <textarea name="optionB" required lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选项C</label>
                <div class="layui-input-inline" style="width: 350px">
                    <textarea name="optionC" required lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">选项D</label>
                    <div class="layui-input-inline" style="width: 350px">
                        <textarea name="optionD" required lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">选择框</label>
                    <div class="layui-input-block">
                        <select name="typ" lay-verify="required">
                            <option  value="1">FPA前15题</option>
                            <option value="2">FPA后15题</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态:</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="status" value="0" lay-skin="switch" lay-text="启用|禁用" checked>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">是否展示</label>
                    <div class="layui-input-block">
                        <input type="radio" name="flag" value="0" title="是">
                        <input type="radio" name="flag" value="1" title="否">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="updateFilter">确认修改</button>
                        <button type="test" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<script src="/layui/layui.js"></script>
<script>
    layui.use(['table', 'form','jquery','laydate'], function(){
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var $ = layui.jquery;
        //第一个实例
        table.render({
            elem: '#demo'
            ,height:500
            ,url: '${pageContext.request.contextPath}/fpa/personalityAllfpa.do' //数据接口
            ,page: true //开启分页
            ,limit:10
            ,limits:[10,20,30,40,50,60,70]
            ,toolbar: '#barDemo'
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
                ,{field: 'question', title: '题目', width:180}
                ,{field: 'optionA', title: '选项A', width:180, sort: true}
                ,{field: 'optionB', title: '选项B', width:180}
                ,{field: 'optionC', title: '选项C', width: 180}
                ,{field: 'optionD', title: '选项D', width: 180, sort: true}
                ,{field: 'typ', title: '题目区分', width: 150,
                        templet: function (d) {
                            //得到当前行数据，并拼接成自定义模板
                            if (d.typ == 1) {
                                //return '可用'
                                return 'FPA前15题'
                            } else if (d.typ == 2) {
                                return 'FPA后15题'
                            } else {
                                return '未知类型'
                            }

                        }
                    }
                ,{field: 'status', title: '状态', width: 80,
                        templet: function (d) {
                            //得到当前行数据，并拼接成自定义模板
                            if (d.status == 0) {
                                //return '可用'
                                return '<a class="layui-btn  layui-btn-xs">可用</a>'
                            } else {
                                return '<a class="layui-btn layui-btn-disabled  layui-btn-xs">失效</a>'
                            }

                        }
            }
                , {field: 'create_time', title: '创建时间', width: 170, sort: true}
                ,{field: 'create_by', title: '添加人', width: 100, sort: true}
                , {field: 'update_time', title: '修改时间', width: 170, sort: true}
                ,{field: 'update_by', title: '修改人', width: 100}
                ,{field: 'flag', title: '显示', width:50, sort: true}
                , {fixed: 'right', title: '操作' ,width: 130, align: 'center', toolbar: '#barDemo1'}
            ]]
        });
        /*
        *
        * 添加题目 唯一性校验
        * */
            $("#questionAdd").blur(function (){
            $.ajax({
                url:"${pageContext.request.contextPath}/fpa/repetitionName.do",
                data: {question: $("#questionAdd").val()},
                type:"GET",
                dataType:"JSON",
                success: function (re) {
                    if (re.code == 0) {
                        $("#questionAdd").next().html(re.msg);
                        $('#addQuestion').removeClass("layui-btn-disabled").attr("disabled", false);
                    } else {
                        $("#questionAdd").next().html(re.msg);
                        $('#addQuestion').addClass("layui-btn-disabled").attr("disabled", true);
                    }
                }
            })

        })
        /**
         * 添加
         */
        form.on('submit(addFilter)', function(data){
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            $.ajax({
                url:"${pageContext.request.contextPath}/fpa/addpersonality.do",
                type:"POST",
                dataType:"JSON",
                data:data.field,
                success:function (ret){
                    if(ret.code==0){
                        layer.closeAll();//关闭对话
                        table.reload('demo',{
                            page: {
                                curr: 1 // 重新加载 从第一页开始
                            }
                        });
                    }
                    layer.msg(ret.msg);
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。

        });
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event) {
                case 'add':
                    layer.open({
                        type: 1,
                        content: $('#addForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                        ,area:['500px','500']
                    });
                    break;
                case 'del':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    }else {
                        var ids = [];
                        for(let i = 0 ; i < data.length;i++) {
                            ids[i] =data[i].id;
                        }
                        layer.confirm('确认删除吗', function (index) {
                            $.ajax({
                                url: "${pageContext.request.contextPath}/fpa/delpersonality.do",
                                type: "POST",
                                dataType: "JSON",
                                data: {id: ids.join(",")},
                                success: function (data) {
                                    if (data.code == 0) {
                                        layer.closeAll();
                                        table.reload('demo');
                                    }
                                    layer.msg(data.msg);
                                }
                            });
                        })
                    }
                    break;
            }
        });
        /**
         * 修改
         */
        form.on('submit(updateFilter)', function(data){
            $.ajax({
                url:"${pageContext.request.contextPath}/fpa/UpdatePersonality.do",
                type:"POST",
                dataType:"JSON",
                data:data.field,
                success:function (ret){
                    if(ret.code==0){
                        layer.closeAll();//关闭对话
                        table.reload('demo',{
                            page: {
                                curr: 1 // 重新加载 从第一页开始
                            }
                        });
                    }
                    layer.msg(ret.msg);
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'update') {
                //updateFormFilter
                console.log("data.id")
                console.log(data.id)
                form.val("updateFormFilter", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id":data.id
                    ,"question": data.question // "name": "value"
                    ,"optionA": data.optionA
                    ,"optionB": data.optionB
                    ,"optionC": data.optionC
                    ,"optionD": data.optionD
                    ,"typ": data.typ
                    ,"status": data.status
                    ,"flag": data.flag
                });
                layer.open({
                    type: 1,
                    content: $('#updateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    , area: ['500px', '350px']
                });
            } else if (layEvent === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    //去后台批量删除数据
                    $.ajax({
                        url: "${pageContext.request.contextPath}/fpa/delpersonality.do",
                        type: "POST",
                        dataType: "JSON",
                        data: {id: data.id},
                        success: function (data) {
                            if (data.code == 0) {
                                //关闭对话框
                                layer.closeAll(); //疯狂模式，关闭所有层
                                //刷新表格数据
                                table.reload('demo');

                            }
                            layer.msg(data.msg);
                        }

                    });
                });
            }
        });
        /**
         * 模糊查询
         */
        form.on('submit(conditionalQuery)', function(data){
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    question: data.field.question //排序字段
                    ,create_by:data.field.create_by//排序方式
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //重置条件查询表单，刷新table数据
        $("#reset").click(function () {
            myTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    question: ""
                    , createBy: ""
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        })
    });

</script>
</body>
</html>
